<?php
/**
* 
*/
class UserCardModel extends MY_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$this->table = 'nmp_user_card';
	}

	//获取列表数据
	public function get_list_data($term='',$page=1,$size=10)
	{
		//查询搜索
		$where = ' where 1 ';
		if (!empty($term['user_id'])) {
			$where .= 'AND c.user_id ='.$term['user_id'];
		}

		if (isset($term['keyword']) && !empty($term['keyword'])) {
			$where .= " AND (u.nickname LIKE '%".$term['keyword']."%' OR cou.name LIKE '%".$term['keyword']."%' OR p.order_sn LIKE '%".$term['keyword']."%' OR p.transaction_id LIKE '%".$term['keyword']."%')";
		}
		if(isset($term['date']) && !empty($term['date'])){
			$date = explode(" - ",$term['date']);
			$date[0] = strtotime($date[0]);
			$date[1] = strtotime($date[1]);
			$where .= " AND (c.addtime<={$date[1]} AND c.addtime>={$date[0]})";
		}

		$sql = "SELECT c.*, u.nickname as user_name, cc.card_name, cou.name as course_name, p.order_sn, p.transaction_id,IFNULL(p.goods_name,'') as goods_name FROM ".$this->table." as c LEFT JOIN 
				 nmp_user as u ON c.user_id=u.id LEFT JOIN 
				 nmp_course_card as cc ON c.card_id=cc.id LEFT JOIN 
				 nmp_course as cou ON c.cid=cou.id LEFT JOIN 
				 nmp_pay_log as p ON p.card_id=c.id 
				 ".$where." ORDER BY c.addtime DESC LIMIT ".($page-1)*$size.",".$size;
		
		$query = $this->db->query($sql);

		$data = $query->result_array();

		foreach ($data as $key => $value) {
			$data[$key]['addtime'] = date("Y-m-d H:i:s",$value['addtime']);
			
			$data[$key]['card_name'] = empty($value['card_name'])?'':$value['card_name'];
			$data[$key]['order_sn'] = empty($value['order_sn'])?'':$value['order_sn'];
			$data[$key]['transaction_id'] = empty($value['transaction_id'])?'':$value['transaction_id'];
		}

		$query = $this->db->query("select COUNT(*) as num from ".$this->table." as c LEFT JOIN 
				 nmp_user as u ON c.user_id=u.id LEFT JOIN  
				 nmp_course as cou ON c.cid=cou.id LEFT JOIN 
				 nmp_pay_log as p ON p.card_id=c.id 
				 ".$where);
		$row = $query->row_array();

		//总数
		$count = $row['num'];
		//总页数
		$pages = ceil($count/$size);
		
		return array('list'=>$data,'count'=>$count, 'pages'=>$pages);
	}
	//获取详情
	public function get_info($id = '')
	{
		$query = $this->db->query("SELECT c.*, u.nickname as user_name, cc.card_name, cou.name as course_name, p.order_sn, p.transaction_id FROM ".$this->table." as c LEFT JOIN 
				 nmp_user as u ON c.user_id=u.id LEFT JOIN 
				 nmp_course_card as cc ON c.card_id=cc.id LEFT JOIN 
				 nmp_course as cou ON c.cid=cou.id LEFT JOIN 
				 nmp_pay_log as p ON p.card_id=c.id 
				 WHERE c.id='{$id}' ");
		$row = $query->row_array();
		$row['addtime'] = date("Y-m-d H:i:s",$row['addtime']);
		
		return $row;
	}

	//获取单行数据
	public function get_row($id = '')
	{
		$this->db->select("* ");
		$this->db->from($this->table);
		$this->db->where('id', $id);

		$query = $this->db->get();

		return $query->row_array();
	}


	//更新数据
	public function updata_data($arr =array(),$id ='')
	{
		if ($this->db->update($this->table,$arr,array('id'=>$id))){			
			return true;
		}else{
			return false;
		}

	}




}

?>